﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace CalculatorLogic.Algebraic_Math
{
    public class Solve : IOperator
    {
        public Equation Apply(Equation equation)
        {
            List<double> collection = new List<double>();
            if (equation.Input[0].Contains("-"))
            {
                equation.Input[0] = equation.Input[0].Replace("-", "+-");
            }
            foreach (string entry in equation.Input[0].Split('='))
            {
                foreach (string val in entry.Split('+'))
                {
                    string temp;
                    if (val.Contains("x"))
                    {
                        temp = val.Replace('x', '1');
                        if (temp.Contains("*"))
                        {
                            temp = (double.Parse(temp.Split('*')[0]) * double.Parse(temp.Split('*')[1])).ToString();
                        }
                        collection.Add(double.Parse(temp));
                    }
                    else
                    {
                        collection.Add(double.Parse(val));
                    }
                }
            }
            
            collection = solver(collection);
            
            if (collection.Count == 2)
            {
                equation.Solved = "x = " + collection[1];
            }
            else
            {
                equation.Solved = "no solution";
            }

            return equation;
        }

        private List<double> solver(List<double> collection)
        {
            List<double> values = new List<double>();
            
            if(collection.Count == 4 && collection[0] - collection[2] == 0)
            {
                return values;
            }
            
            if (collection.Count == 4)
            {
                values.Add(collection[0] - collection[2]);
                values.Add(collection[3] - collection[1]);
                if (values[0] != 1)
                {
                    values[1] = values[1] / values[0];
                }
            }
            else if(collection.Count == 3)
            {
                values.Add(collection[0]);
                values.Add(collection[2] - collection[1]);
                if (values[0] != 1)
                {
                    values[1] = values[1] / values[0];
                }
            }
            else if (collection.Count == 2)
            {
                values.Add(collection[0]);
                values.Add(collection[1]);
                if (values[0] != 1)
                {
                    values[1] = values[1] / values[0];
                }
            }
            
            return values;
        }
    }
}
